package com.title1;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

/**
 * @Author milian
 * @Date 2021/8/10 0010 19:36
 */
public class uploadThread extends Thread{
    private Socket accept;
    public uploadThread(Socket accept){
        this.accept = accept;
    }
    @Override
    public void run() {
        BufferedInputStream bufferedInputStream = null;
        try {
             bufferedInputStream = new BufferedInputStream(accept.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("H:\\homework\\day16\\src\\com\\title1\\" + UUID.randomUUID() + ".jpg"));
            int b;
            while ((b=bufferedInputStream.read())!=-1){
                bos.write(b);
            }
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功！");
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
